package com.cakebackoffice.clientsoap.contract;


import com.cakebackoffice.Model.CatalogCategory;
import org.ksoap2.serialization.NullSoapObject;
import org.ksoap2.serialization.SoapObject;

public class ProductCategory extends ContractObject {
    public int id;
    public String name;
    public String illustration;
    public String description;
    public SoapArray<Integer> childCategoryId;
    public SoapArray<Integer> childProductsId;

    public ProductCategory(CatalogCategory actualCategory) {
        if (actualCategory.getRemoteID() != null)
            id = Integer.valueOf(actualCategory.getRemoteID().toString());
        name = actualCategory.getName().trim();
        illustration = actualCategory.getIllustration();
        description = actualCategory.getDescription();
        childCategoryId = new SoapArray<Integer>(actualCategory.getChildCategoriesId().size(), "childCategoryId");
        for (Long id : actualCategory.getChildCategoriesId()) {
            childCategoryId.add(Integer.valueOf(id.toString()));
        }
        childProductsId = new SoapArray<Integer>(actualCategory.getChildProductTypeId().size(), "childProductsId");
        for (Long id : actualCategory.getChildProductTypeId()) {
            childProductsId.add(Integer.valueOf(id.toString()));
        }
    }

    public ProductCategory() {

    }

    public void fill(SoapObject soapObject) {
        this.id = Integer.parseInt(soapObject.getPropertyAsString("id"));
        this.name = soapObject.getPropertySafelyAsString("name").trim();
        this.illustration = soapObject.getPropertySafelyAsString("illustration");
        this.description = soapObject.getPropertySafelyAsString("description");

        SoapObject s = soapObject.getPropertySafely("childCategoryId") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("childCategoryId");
        if (s != null) {
            childCategoryId = new SoapArray<Integer>(s.getPropertyCount(), "childCategoryId");
            for (int i = 0; i < s.getPropertyCount(); i++) {
                childCategoryId.add(Integer.parseInt(s.getProperty(i).toString()));
            }
        }

        SoapObject localPricesSoap = soapObject.getPropertySafely("childProductsId") instanceof NullSoapObject ? null : (SoapObject) soapObject.getPropertySafely("childProductsId");
        if (localPricesSoap != null) {
            childProductsId = new SoapArray<Integer>(localPricesSoap.getPropertyCount(), "childProductsId");
            for (int i = 0; i < localPricesSoap.getPropertyCount(); i++) {
                childProductsId.add(Integer.parseInt(localPricesSoap.getProperty(i).toString()));
            }
        }
    }
}
